package org.nci.platform.workflow.service.impl;

import org.flowable.common.engine.impl.service.CommonEngineServiceImpl;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.nci.platform.workflow.entity.SysDeployForm;
import org.nci.platform.workflow.entity.SysForm;
import org.nci.platform.workflow.mapper.SysDeployFormMapper;
import org.nci.platform.workflow.service.FlowableMapperService;
import org.nci.platform.workflow.service.ISysDeployFormService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 流程实例关联表单Service业务层处理
 *
 * @author Tony
 * @date 2021-04-03
 */
@Service
public class SysDeployFormServiceImpl extends CommonEngineServiceImpl<ProcessEngineConfigurationImpl> implements ISysDeployFormService {
    @Resource
    private FlowableMapperService flowableMapperService;

    /**
     * 查询流程实例关联表单
     *
     * @param id 流程实例关联表单ID
     * @return 流程实例关联表单
     */
    @Override
    public SysDeployForm selectSysDeployFormById(Long id) {
        return flowableMapperService.executeQuery(SysDeployFormMapper.class, mapper -> mapper.selectSysDeployFormById(id));
    }

    /**
     * 查询流程实例关联表单列表
     *
     * @param sysDeployForm 流程实例关联表单
     * @return 流程实例关联表单
     */
    @Override
    public List<SysDeployForm> selectSysDeployFormList(SysDeployForm sysDeployForm) {
        return flowableMapperService.executeQuery(SysDeployFormMapper.class, mapper -> mapper.selectSysDeployFormList(sysDeployForm));
    }

    /**
     * 新增流程实例关联表单
     *
     * @param sysDeployForm 流程实例关联表单
     * @return 结果
     */
    @Override
    public int insertSysDeployForm(SysDeployForm sysDeployForm) {
        SysForm sysForm = flowableMapperService.executeQuery(SysDeployFormMapper.class, mapper -> mapper.selectSysDeployFormByDeployId(sysDeployForm.getDeployId()));
        if (Objects.isNull(sysForm)) {
            flowableMapperService.executeUpdate(SysDeployFormMapper.class, mapper -> mapper.insertSysDeployForm(sysDeployForm));
            return 0;
        } else {
            return 1;
        }
    }

    /**
     * 修改流程实例关联表单
     *
     * @param sysDeployForm 流程实例关联表单
     * @return 结果
     */
    @Override
    public int updateSysDeployForm(SysDeployForm sysDeployForm) {
        flowableMapperService.executeUpdate(SysDeployFormMapper.class, mapper -> mapper.updateSysDeployForm(sysDeployForm));
        return 1;
    }

    /**
     * 批量删除流程实例关联表单
     *
     * @param ids 需要删除的流程实例关联表单ID
     * @return 结果
     */
    @Override
    public int deleteSysDeployFormByIds(Long[] ids) {
        flowableMapperService.executeUpdate(SysDeployFormMapper.class, mapper -> mapper.deleteSysDeployFormByIds(ids));
        return 1;
    }

    /**
     * 删除流程实例关联表单信息
     *
     * @param id 流程实例关联表单ID
     * @return 结果
     */
    @Override
    public int deleteSysDeployFormById(Long id) {
        flowableMapperService.executeUpdate(SysDeployFormMapper.class, mapper -> mapper.deleteSysDeployFormById(id));
        return 1;
    }

    /**
     * 查询流程挂着的表单
     *
     * @param deployId
     * @return
     */
    @Override
    public SysForm selectSysDeployFormByDeployId(String deployId) {
        return flowableMapperService.executeQuery(SysDeployFormMapper.class, mapper -> mapper.selectSysDeployFormByDeployId(deployId));
    }
}
